import { Http } from '@angular/http';
import { InventoryService } from './../../service/inventory.service';
import { Order } from './../../model/clerkDataContext';
import { IMessageHandle, IMessageStruct } from './msgHandleMg';

export class OrderMessageHandle implements IMessageHandle {
    get MessageName() { return "orders"; }
    get HandleName() { return "OrderMessageHandle"; }
    OnReceived(sender, args: IMessageStruct) {
        this.OnMessage && this.OnMessage(args.body);
    }

    OnMessage: (order: Order) => void;
}

export class PreOrderMessageHandle implements IMessageHandle {
    private http: Http;
    get MessageName() { return "preOrders"; }
    get HandleName() { return "PreOrderMessageHandle"; }
    async OnReceived(sender, args: IMessageStruct) {
        try {
            let invSvr = new InventoryService(this.http);
            let preOrder: Order = args.body;
            for (let item of preOrder.cart.items) {
                let inventory = await invSvr.GetInventoryById(item.inventoryId);
                item.inventory = inventory;
            }
            this.OnMessage && this.OnMessage(preOrder);
        } catch (error) {
            console.log("ERROR:", error);
        }

    }

    OnMessage: (order: Order) => void;
}